/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller.compound;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.dto.CompoundDto;
import com.nai365.nart.operation.entity.CompoundRecord;
import com.nai365.nart.operation.service.CompoundRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 合成记录
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/compound")
@Api(value = "compound", tags = "藏品合成")
public class CompoundRecordController {

	private final CompoundRecordService compoundRecordService;

	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param compoundRecord 合成记录
	 * @return
	 */
	@ApiOperation(value = "合成记录分页查询", tags = {"藏品合成-PC端"})
	@GetMapping("/record/page")
	public R<Page<CompoundRecord>> getCompoundRecordPage(Page<CompoundRecord> page, CompoundRecord compoundRecord) {
		return R.ok(compoundRecordService.page(page, Wrappers.query(compoundRecord)));
	}

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @return R<Page < CompoundRecord>>
	 */
	@ApiOperation(value = "我的合成记录", tags = {"藏品合成-移动端"})
	@GetMapping("/my/record")
	public R<Page<CompoundRecord>> myCompoundRecordPage(Page<CompoundRecord> page, @ApiParam("合成需求Id") Long demandId) {
		LambdaQueryWrapper<CompoundRecord> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(CompoundRecord::getUserId, SecurityUtils.getUser().getId());
		if (demandId != null) {
			queryWrapper.eq(CompoundRecord::getCompoundDemandId, demandId);
		}
		return R.ok(compoundRecordService.page(page, queryWrapper));
	}


	/**
	 * 通过id查询合成记录
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "合成记录详情", tags = {"藏品合成-移动端"})
	@GetMapping("/{id}")
	public R<CompoundRecord> getById(@PathVariable("id") @ApiParam("合成记录id") Long id) {
		return R.ok(compoundRecordService.getById(id));
	}

	/**
	 * 新增合成记录
	 *
	 * @param compoundDto 合成dto
	 * @return R
	 */
	@ApiOperation(value = "合成", tags = {"藏品合成-移动端"})
	@SysLog("新增合成记录")
	@PostMapping
	public R<CompoundRecord> save(@RequestBody CompoundDto compoundDto) {
		try {
			return R.ok(compoundRecordService.compound(compoundDto), "合成成功");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}
}
